/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package com.xjw.tool;

import com.xjw.entity.ChessPoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xwx1052336
 * @since 2021/10/12
 */
public class ArrayUtil {

    public static void main(String[] args) {
        int [] []  ary = new int [15][15];
        ary[0][1] = 1;
        ary[1][2] = 2;
        ary[2][3] = 2;
        ary[3][4] = 1;
        ary[4][0] = 2;
        String src = toString(ary);
        int[][] arry = stringToArray(src, ary.length, ary[0].length);
        System.out.println(toString(arry));
        System.out.println(arry.length+":"+arry[0].length);
    }

    /**
     * 二维数组转字符串
     *
     * @param ary 数组
     * @return 字符串
     */
    public static String toString(int [][] ary){
        StringBuilder stb = new StringBuilder("[");
        for (int[] ints : ary) {
            stb.append(Arrays.toString(ints)).append(",");
        }
        stb.deleteCharAt(stb.length()-1);
        stb.append("]");
        return stb.toString();
    }

    /**
     * 字符串转int二维数组（int数据为个位数）
     * @param src 需要转化的字符串
     * @param length 一维长度
     * @param arrL 二维长度
     * @return int[][]
     */
    public static int[][] stringToArray(String src, int length, int arrL){
        src = src.substring(1,src.length()-1);
        int [][] arry = new int[length][arrL];
        String regex = "(?<=\\[).{"+(3*arrL-2)+","+(4*arrL-2)+"}(?=])";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(src);
        int index = 0;
        while (matcher.find()){
            int arrIndex = 0;
            for(String numStr:matcher.group(0).split(",")){
                arry[index][arrIndex++] = Integer.parseInt(numStr.trim());
            }
            index++;
        }
        return arry;
    }

    public static double [] changeArryDimension(int [][] arry){
        double [] result = new double[arry.length*arry[0].length];
        int index = 0;
        for(int [] ary:arry) {
            for (int num:ary) {
                result[index++] = num;
            }
        }
        return result;
    }

    public static List<ChessPoint> getAllChess(char [][] arry, char chessColor){
        List<ChessPoint> chessPoints = new ArrayList<>(arry.length*arry[0].length/2+1);
        for(int i = 0;i< arry.length;i++){
            for(int j = 0;j<arry[0].length;j++){
                if(arry[i][j] == chessColor){
                    ChessPoint chessPoint = new ChessPoint();
                    chessPoint.setPointX(i);
                    chessPoint.setPointY(j);
                    chessPoint.setChessColor(chessColor);
                    chessPoints.add(chessPoint);
                }
            }
        }
        return chessPoints;
    }

    public static char[][] intArrToChar(int [][] arry){
        char[][] result = new char[arry.length + 1][arry[0].length + 1];
        for(int i = 0; i < arry.length;i++){
            for(int j = 0;j < arry[0].length;j++){
                switch (arry[i][j]){
                    case 0:result[i+1][j+1]= ' ';break;
                    case 1:result[i+1][j+1]= 'B';break;
                    case 2:result[i+1][j+1]= 'W';break;
                }
            }
        }
        return result;
    }

}
